package net.hoobo.lookzw.repository.impl;

import net.hoobo.lookzw.domain.VisitRecord;
import net.hoobo.lookzw.repository.VisitRecordDao;
import net.hoobo.lookzw.domain.VisitRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class VisitRecordDaoImpl implements VisitRecordDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveRecord(VisitRecord record) {
        mongoTemplate.save(record);
    }

    @Override
    public List<VisitRecord> findRecordById(Long id, Date date) {
        Criteria criteria = Criteria.where("user_id").is(id);
        if (date!=null){
            GregorianCalendar gcal = new GregorianCalendar();
            gcal.setTime(date);
            gcal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            gcal.set(Calendar.HOUR_OF_DAY,0);
            gcal.set(Calendar.MINUTE,0);
            gcal.set(Calendar.SECOND,0);
            GregorianCalendar gcal1 = new GregorianCalendar();
            gcal1.setTime(gcal.getTime());
            gcal1.add(Calendar.DATE,1);
            criteria.and("start_time").gte(gcal.getTime()).lt(gcal1.getTime());
        }
        Query query=new Query(criteria);
        List<VisitRecord> record =  mongoTemplate.find(query,VisitRecord.class);
        return record;
    }


    @Override
    public void updateRecord(VisitRecord record) {
//        Query query=new Query(Criteria.where("id").is(record.getId()));
//        Update update= new Update().set("recordName", record.getName()).set("passWord", record.getDescription());
//        //更新查询返回结果集的第一条
//        mongoTemplate.updateFirst(query,update, VisitRecord.class);
//        //更新查询返回结果集的所有
//        // mongoTemplate.updateMulti(query,update,VisitRecord.class);
    }

    @Override
    public void removeRecord(Long id) {
//        Query query=new Query(Criteria.where("user_id").is(id));
//        mongoTemplate.remove(query, VisitRecord.class);
    }
}
